package com.ruoyi.finance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.finance.domain.VoucherTemplate;
import com.ruoyi.finance.domain.VoucherTemplateType;
import com.ruoyi.finance.vo.IncomingDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.finance.domain.IncomingInvoice;
import com.ruoyi.finance.service.IIncomingInvoiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销项发票Controller
 *
 * @author ruoyi
 * @date 2025-03-04
 */
@RestController
@RequestMapping("/finance/invoice")
public class IncomingInvoiceController extends BaseController
{
    @Autowired
    private IIncomingInvoiceService incomingInvoiceService;

    /**
     * 查询销项发票列表
     */
    @PreAuthorize("@ss.hasPermi('finance:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(IncomingInvoice incomingInvoice)
    {
        startPage();
        List<IncomingInvoice> list = incomingInvoiceService.selectIncomingInvoiceList(incomingInvoice);
        return getDataTable(list);
    }

    /**
     * 导出销项发票列表
     */
    @PreAuthorize("@ss.hasPermi('finance:invoice:export')")
    @Log(title = "销项发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IncomingInvoice incomingInvoice)
    {
        List<IncomingInvoice> list = incomingInvoiceService.selectIncomingInvoiceList(incomingInvoice);
        ExcelUtil<IncomingInvoice> util = new ExcelUtil<IncomingInvoice>(IncomingInvoice.class);
        util.exportExcel(response, list, "销项发票数据");
    }

    /**
     * 获取销项发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:invoice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(incomingInvoiceService.selectIncomingInvoiceById(id));
    }

    /**
     * 新增销项发票
     */
    @PreAuthorize("@ss.hasPermi('finance:invoice:add')")
    @Log(title = "销项发票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IncomingInvoice incomingInvoice)
    {
        return toAjax(incomingInvoiceService.insertIncomingInvoice(incomingInvoice));
    }

    /**
     * 修改销项发票
     */
    @PreAuthorize("@ss.hasPermi('finance:invoice:edit')")
    @Log(title = "销项发票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IncomingInvoice incomingInvoice)
    {
        return toAjax(incomingInvoiceService.updateIncomingInvoice(incomingInvoice));
    }

    /**
     * 删除销项发票
     */
    @PreAuthorize("@ss.hasPermi('finance:invoice:remove')")
    @Log(title = "销项发票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(incomingInvoiceService.deleteIncomingInvoiceByIds(ids));
    }

    //TODO 获取凭证模板业务类型
    @GetMapping("/VoucherTemplateTypelist")
    public AjaxResult VoucherTemplateTypelist(){
        List<VoucherTemplateType> VoucherTemplateTypelist=incomingInvoiceService.VoucherTemplateTypelist();
        return AjaxResult.success(VoucherTemplateTypelist);

    }

    //TODO 获取凭证模板名称
    @GetMapping("/VoucherTemplate")
    public AjaxResult VoucherTemplatelist(){
        List<VoucherTemplate>VoucherTemplatelist=incomingInvoiceService.VoucherTemplatelist();
        return AjaxResult.success(VoucherTemplatelist);
    }

    //TODO 新增销项发票
    @PostMapping("/incomingInvoiceadd")
    public AjaxResult incomingInvoiceadd(IncomingDTO incomingDTO){

        return AjaxResult.success(incomingInvoiceService.incomingInvoice(incomingDTO));
    }


}
